<?php
/**
* @author zhaorongwei 
* @date 2012-06-19
* @description 权限相关
*/

class AuthController extends Controller
{
	public function __construct()
	{
		$this->model=loadModel('auth');
	}

	//添加功能
	public function functionAdd()
	{
		$vars = array(
			'pageTitle' => '添加功能'
			);
		$parentid=0;	
		if(isPost())
		{
			$data = post('data');
			$parentid=$data['fparent_id'];
			if($data['fname_en'] == '')
			{
				$vars['errors']='请输入功能名称';
			}
			else
			{
				$data['fname_en']=strtolower($data['fname_en']);
				if($this->model->functionExists($data['fparent_id'],$data['fname_en']))
				{
					$vars['errors']='功能已存在';
				}
				else
				{
					if($this->model->functionAdd($data))
					{
						$vars['errors']='添加成功';
					}
				}
			}
		}
		$vars['function_select'] = treeSelect(formatTreeData($this->model->functionlist()),$parentid);
		template('auth/functionadd', $vars);
	}
	
	//修改功能
	public function functionEdit()
	{
		$vars = array(
			'pageTitle' => '修改功能'
			);
		$id=intval(request('id'));
		if($id<1)
		{
			redirect('auth/functionlist');
		}
		if(isPost())
		{
			$data = post('data');
			if($data['fname_en'] == '')
			{
				$vars['errors']='请输入功能名称';
			}
			else
			{
				$data['fname_en']=strtolower($data['fname_en']);
				if($this->model->functionEdit($id,$data))
				{
					redirect('auth/functionlist');
				}
			}
		}
		$vars['info'] = $this->model->getFunctionById($id);
		$vars['function_select'] = treeSelect(formatTreeData($this->model->functionlist()),$vars['info']['fparent_id']);
		template('auth/functionedit', $vars);
	}
	
	//列表
	public function functionList()
	{
		$vars = array(
			'pageTitle' => '功能列表'
			);
		$vars['function_ui'] = treeUi(formatTreeData($this->model->functionList()), true);
		template('auth/functionlist', $vars);
	}

	
	public function functionDel()
	{
		$id=intval(get('id'));
		if($id>0)
		{
			$this->model->functionDel($id);
		}
		redirect('auth/functionlist');
	}	
	
	//用户授权
	public function setAuth()
	{
		$vars = array(
			'pageTitle' => '设置权限'
			);
		$userModel = $this->loadModel('user');
		if(isPost())
		{
			$fids = post('function_id');
			if(is_array($fids))
			{
				$fids = ','.join(',', $fids).',';
			}
			
			$gid=post('gid');
			$userModel->usergroupAuth($gid,$fids);
			redirect('auth/setauth', 'id='.$gid);
		}
		
		$vars['function_table'] = treeTable(formatTreeData($this->model->functionList()));
		$group_info=$userModel->getUserGroupById(intval(get('id')));
		$vars['fids'] = explode(',', trim($group_info['ffunctionids'], ','));
		$vars['gid'] = $group_info['fid'];
		template('auth/setauth', $vars);
	}
}
